package com.changgou.order.listener;

import com.alibaba.fastjson.JSON;
import com.changgou.order.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Map;

@Component
@RabbitListener(queues = "${mq.pay.queue.order}")
public class OrderMessageListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void getMessage(String message) throws ParseException {
        Map<String,String> resultMap = JSON.parseObject(message, Map.class);
        System.out.println("监听到的消息:" + resultMap);

        String return_code = resultMap.get("return_code");

        if (return_code.equals("SUCCESS")) {
            String result_code = resultMap.get("result_code");
            String orderId = resultMap.get("out_trade_no");

            if (result_code.equals("SUCCESS")) {
                orderService.updateStataus(orderId,  resultMap.get("time_end"), resultMap.get("transaction_id"));
            }else{
                //todo
                orderService.delete(orderId);
            }
        }

    }
}
